package fr.utbm.tx52.stamp.ui.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import fr.utbm.tx52.stamp.R;
import fr.utbm.tx52.stamp.ui.activity.Main;

/**
 * Class which is used to display a dialog window when the app is first launch
 * to chose the mode for the app
 *
 * @author GREA Antoine
 * @author TISSERAND Geoffrey
 */
public class Mode extends DialogFragment
{

	private static Main mContext;

	/**
	 * Create a new instance of ModeDialog
	 */
	public static Mode newInstance(Main context)
	{
		Mode f = new Mode();
		Mode.mContext = context;
		// Supply num input as an argument.
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// Pick a style based on the num.
		int style = DialogFragment.STYLE_NORMAL, theme = 0;
		setStyle(style, theme);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		final CharSequence[] levelItems =
		{
			getString(R.string.level_low), getString(R.string.level_medium)
		};

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_DARK);
		builder.setTitle(R.string.levelTitle);
		builder.setSingleChoiceItems(levelItems, -1,
			new DialogInterface.OnClickListener()
			{

				public void onClick(DialogInterface dialog, int level)
				{
					SharedPreferences preferences = mContext.getPreferences(Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = preferences.edit();
					editor.putInt(Main.PREFERENCE_LEVEL_KEY, ((level == 0) ? Main.Level.LOW.ordinal() : Main.Level.MEDIUM.ordinal()));
					editor.commit();
					dismiss();
					mContext.draw();
				}
			});
		return builder.create();
	}
}
